<template>
  <div class="app-container">
    <el-table
      v-if="refreshTable"
      v-loading="loading"
      :data="regionList"
      row-key="id"
      lazy
      :load="load"
      :default-expand-all="isExpandAll"
      :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
    >
      <el-table-column label="名称" align="center" prop="name" width="200" />
      <el-table-column label="简称" align="center" prop="shortName" />
      <el-table-column label="省" align="center" prop="province" />
      <el-table-column label="市" align="center" prop="city" />
      <el-table-column label="区" align="center" prop="district" />
      <el-table-column label="拼音" align="center" prop="pinyin" />
      <el-table-column label="区号" align="center" prop="cityCode" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:region:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-plus"
            @click="handleAdd(scope.row)"
            v-hasPermi="['system:region:add']"
            >新增</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:region:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加或修改省市区管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="ID" prop="id">
          <el-input v-model="form.id" placeholder="请输入ID" />
        </el-form-item>
        <el-form-item label="父级ID" prop="parentId">
          <treeselect
            v-model="form.parentId"
            :options="regionOptions"
            :normalizer="normalizer"
            placeholder="请选择父级ID"
          />
        </el-form-item>
        <el-form-item label="名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入名称" />
        </el-form-item>
        <el-form-item label="简称" prop="shortName">
          <el-input v-model="form.shortName" placeholder="请输入简称" />
        </el-form-item>
        <el-form-item label="路径" prop="parentPath">
          <el-input v-model="form.parentPath" placeholder="请输入路径" />
        </el-form-item>
        <el-form-item label="省" prop="province">
          <el-input v-model="form.province" placeholder="请输入省" />
        </el-form-item>
        <el-form-item label="市" prop="city">
          <el-input v-model="form.city" placeholder="请输入市" />
        </el-form-item>
        <el-form-item label="区" prop="district">
          <el-input v-model="form.district" placeholder="请输入区" />
        </el-form-item>
        <el-form-item label="省简称" prop="provinceShortName">
          <el-input
            v-model="form.provinceShortName"
            placeholder="请输入省简称"
          />
        </el-form-item>
        <el-form-item label="市简称" prop="cityShortName">
          <el-input v-model="form.cityShortName" placeholder="请输入市简称" />
        </el-form-item>
        <el-form-item label="区简称" prop="districtShortName">
          <el-input
            v-model="form.districtShortName"
            placeholder="请输入区简称"
          />
        </el-form-item>
        <el-form-item label="省拼音" prop="provincePinyin">
          <el-input v-model="form.provincePinyin" placeholder="请输入省拼音" />
        </el-form-item>
        <el-form-item label="市拼音" prop="cityPinyin">
          <el-input v-model="form.cityPinyin" placeholder="请输入市拼音" />
        </el-form-item>
        <el-form-item label="区拼音" prop="districtPinyin">
          <el-input v-model="form.districtPinyin" placeholder="请输入区拼音" />
        </el-form-item>
        <el-form-item label="拼音" prop="pinyin">
          <el-input v-model="form.pinyin" placeholder="请输入拼音" />
        </el-form-item>
        <el-form-item label="简拼" prop="jianpin">
          <el-input v-model="form.jianpin" placeholder="请输入简拼" />
        </el-form-item>
        <el-form-item label="首字母" prop="firstChar">
          <el-input v-model="form.firstChar" placeholder="请输入首字母" />
        </el-form-item>
        <el-form-item label="城市编码" prop="cityCode">
          <el-input v-model="form.cityCode" placeholder="请输入城市编码" />
        </el-form-item>
        <el-form-item label="邮政编码" prop="zipCode">
          <el-input v-model="form.zipCode" placeholder="请输入邮政编码" />
        </el-form-item>
        <el-form-item label="维度" prop="lng">
          <el-input v-model="form.lng" placeholder="请输入维度" />
        </el-form-item>
        <el-form-item label="经度" prop="lat">
          <el-input v-model="form.lat" placeholder="请输入经度" />
        </el-form-item>
        <el-form-item label="备注1" prop="remark1">
          <el-input v-model="form.remark1" placeholder="请输入备注1" />
        </el-form-item>
        <el-form-item label="备注2" prop="remark2">
          <el-input v-model="form.remark2" placeholder="请输入备注2" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listRegion,
  getRegion,
  delRegion,
  addRegion,
  updateRegion,
} from "@/api/system/region";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  name: "Region",
  components: {
    Treeselect,
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 省市区管理表格数据
      regionList: [],
      // 省市区管理树选项
      regionOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否展开，默认全部展开
      isExpandAll: true,
      // 重新渲染表格状态
      refreshTable: true,
      // 查询参数
      queryParams: {
        parentId: null,
        levelType: null,
        name: null,
        shortName: null,
        parentPath: null,
        province: null,
        city: null,
        district: null,
        provinceShortName: null,
        cityShortName: null,
        districtShortName: null,
        provincePinyin: null,
        cityPinyin: null,
        districtPinyin: null,
        pinyin: null,
        jianpin: null,
        firstChar: null,
        cityCode: null,
        zipCode: null,
        lng: null,
        lat: null,
        remark1: null,
        remark2: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        id: [{ required: true, message: "ID不能为空", trigger: "blur" }],
      },
    };
  },
  created() {
    this.getList({ parentId: 100000 });
  },
  methods: {
    /** 查询省市区管理列表 */
    getList(query) {
      this.loading = true;
      listRegion(query).then((response) => {
        this.regionList = this.handleTree(response.data, "id", "parentId");
        this.loading = false;
      });
    },
    /** 转换省市区管理数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.id,
        label: node.name,
        children: node.children,
      };
    },
    /** 查询省市区管理下拉树结构 */
    getTreeselect() {
      listRegion().then((response) => {
        this.regionOptions = [];
        const data = { id: 0, name: "顶级节点", children: [] };
        data.children = this.handleTree(response.data, "id", "parentId");
        this.regionOptions.push(data);
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        parentId: null,
        levelType: null,
        name: null,
        shortName: null,
        parentPath: null,
        province: null,
        city: null,
        district: null,
        provinceShortName: null,
        cityShortName: null,
        districtShortName: null,
        provincePinyin: null,
        cityPinyin: null,
        districtPinyin: null,
        pinyin: null,
        jianpin: null,
        firstChar: null,
        cityCode: null,
        zipCode: null,
        lng: null,
        lat: null,
        remark1: null,
        remark2: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd(row) {
      this.reset();
      this.getTreeselect();
      if (row != null && row.id) {
        this.form.parentId = row.id;
      } else {
        this.form.parentId = 0;
      }
      this.open = true;
      this.title = "添加省市区管理";
    },
    /** 展开/折叠操作 */
    toggleExpandAll() {
      this.refreshTable = false;
      this.isExpandAll = !this.isExpandAll;
      this.$nextTick(() => {
        this.refreshTable = true;
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.getTreeselect();
      if (row != null) {
        this.form.parentId = row.id;
      }
      getRegion(row.id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改省市区管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateRegion(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addRegion(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$modal
        .confirm('是否确认删除省市区管理编号为"' + row.id + '"的数据项？')
        .then(function () {
          return delRegion(row.id);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
  },
};
</script>
